#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn][maxn];
LL dp[maxn][maxn];
LL ans=1e18;
LL h,w,C;
void solve(){
     memset(dp,0x3f,sizeof(dp));
     for(LL i=1;i<=h;i++){
        for(LL j=1;j<=w;j++){
            dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
            ans=min(ans,dp[i][j]+a[i][j]+C*(i+j));
            dp[i][j]=min(dp[i][j],a[i][j]-C*(i+j));
        }
     }
}
int main(void){
   	cin.tie(0);std::ios::sync_with_stdio(false);
    cin>>h>>w>>C;
    for(LL i=1;i<=h;i++){
        for(LL j=1;j<=w;j++){
            cin>>a[i][j];
        }
    }
    solve();
    for(LL i=1;i<=h;i++){
        LL l=1;LL r=w;
        while(l<r){
            swap(a[i][l],a[i][r]);
            l++;r--;
        }
    }
    solve();
    cout<<ans<<"\n";
   	return 0;
}
